package com.netease.xone.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.LinearLayout;

import com.netease.pkgRelated.XoneConsts;
import com.netease.pkgRelated.XoneConsts.BUNDLE_KEY;
import com.netease.pkgRelated.XoneConsts.WB_LOGIN_TYPE;
import com.netease.xone.R;
import com.netease.xone.fragment.FragmentWBLogin;

public class ActivityWBBind extends ActivityX1Base {
    private FragmentWBLogin mFragmentWBLogin;
	/**
	 * 绑定之后返回之前界面
	 * @param context
	 * @param type
	 */
	public static void launch(Context context, int type) {
		Intent i = new Intent(context, ActivityWBBind.class);
		i.putExtra(BUNDLE_KEY.WB_BIND_TYPE, type);
		context.startActivity(i);
	}
	
	public static void launch(Fragment frag, int type) {
        Intent i = new Intent(frag.getActivity(), ActivityWBBind.class);
        i.putExtra(BUNDLE_KEY.WB_BIND_TYPE, type);
        frag.startActivityForResult(i, XoneConsts.RESULT_BASE);
    }
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LinearLayout linear = new LinearLayout(this);
		linear.setId(R.id.activity_WBBind_container_id);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);
		linear.setLayoutParams(lp);
		setContentView(linear);
		Intent intent = getIntent();
        int type = 0;
        if(intent != null) {
            type = intent.getIntExtra(BUNDLE_KEY.WB_BIND_TYPE, 0);
        }
        if(type <= 0 ){
            finish();
            return;
        }
		if (findViewById(R.id.activity_WBBind_container_id) != null
				&& savedInstanceState == null) {
			FragmentManager fm = getSupportFragmentManager();
			FragmentTransaction ft = fm.beginTransaction();
			mFragmentWBLogin = FragmentWBLogin.newInstance(WB_LOGIN_TYPE.Bind, type);
			ft.add(R.id.activity_WBBind_container_id, mFragmentWBLogin);
			ft.commit();
		}
	}
	  @Override
	    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	        super.onActivityResult(requestCode, resultCode, data);

	        if (mFragmentWBLogin != null && mFragmentWBLogin.getSsoHandler() != null) {
	            mFragmentWBLogin.getSsoHandler().authorizeCallBack(requestCode, resultCode, data);
	        }
	    }

}
